+function(app) {

	var formSubmit = function(form, fn) {
		var method = form.attr("method");
		var url = form.attr("action");
		var pp = $.getPageParams();
		var data = $.extend({}, pp, form.serializeJson());
		$.ajax({
			type : method,
			url : url,
			data : data,
			dataType: "json",
			success : function(json) {
				if (json.code == "SUCCESS") {
					fn && fn(json);
				} else {
					// 登录失败, 显示提示消息
					form.find(".prompt-message").text(json.message);
				}
			}
		});
	};

	// 登录
	$(function() {
		var form = $("form[name=login]");
		if (form.length == 0) { return; }

		var inputs = form.find("input[type=text],input[type=password]");

		var submit = function(e) {
			e.preventDefault();
			// 必填校验, login页面的校验比较简单没有用zhhvalidate
			for (var i = 0; i < inputs.length; i++) {
				var me = inputs.eq(i);
				if (!me.val()) { return app.showRequiredPrompt(me, true); }
			}
			// 提交
			formSubmit(form, function(json) {
				var account = form.find("input[name=account]").val();
				window.Cookies && Cookies.set("AUTH_ACCOUNT", account, { path:app.getCookiePath() });
				window.Cookies && Cookies.remove("TEMP_ACCOUNT", { path:app.getCookiePath() });
				app.toMainPage.call(form, json);
			});
		};

		// 绑定事件-提交
		form.find(".do-submit").click(submit);

		// 回车键移到下一输入框, 最后一个回车提交
		Utils.enterToNext(inputs, submit);

		inputs.zchanged(function(n, o) {
			// 输入框有变更时清空错误提示
			form.find(".prompt-message").text("");
			if (this.name == "account") {
				var options = { path:app.getCookiePath() };
				// 登录页面没有引用zhh.validate.js
				// if (window.Cookies && $.fn.zhhvalidate.defaults.regexp.mobile.test(n)) {
				if (/^1[0-9]{10}$/.test(n)) {
					window.Cookies && Cookies.set("TEMP_ACCOUNT", n, $.extend({ expires:app.getCookieExpires( 10 * 60 ) }, options));
				} else {
					window.Cookies && Cookies.remove("TEMP_ACCOUNT", options);
				}
			}
			var me = $(this);
			var placeholder = me.attr("placeholder");
			if (!placeholder) { // 通过label模拟实现的placeholder
				var label = $([]);
				var id = me.attr("id");
				if (id) { label = form.find("label[for=" + id + "]"); }
				if (n == "") {
					label.show();
				} else {
					label.hide();
				}
			}
		});

		if (window.Cookies) {
			var account = Cookies.get("TEMP_ACCOUNT") || Cookies.get("AUTH_ACCOUNT");
			account && form.find("input[name=account]").val(account);
		}
	});

}(window.app);
